#include "Game.h"
#include "Messenger.h"
#include "Gfx.h"
#include "Constants.h"
#include "Lang.h"

Game game;

/* Disable jackpot when in raise score mode ... */
int nojackpot = 0;

/* Score management */
void score_incr (long value) 
{
	static Uint32 ticks = 999999999;
	static Uint32 now;
	static long time_bonus = TIME_BONUS_BASE;
	static int bonus_count = 0;
	static int jackpot_count = 1;

	/* Nothing to do ! */
	if ( value == 0 ) return;

if ( nojackpot == 0 )
{

	double thres;
	int    decount_thres = 4;

	thres = 1000.0 * 5.0 / (double)game.speed;
	now = SDL_GetTicks();
	if ( now - ticks < thres)
	{
		bonus_count ++;
	}else{

		if ( bonus_count >= 8 )
		{
			message_noint (STR_JACKPOT_MISSED);
		}

		bonus_count = 0;
		time_bonus  = TIME_BONUS_BASE;
		jackpot_count = 1;
	}

	ticks = now;

	if ( jackpot_count > 1 ) decount_thres = 0;
	else			 decount_thres = 4;
	
	if ( bonus_count >= decount_thres && bonus_count < 10 )
	{
		char str[32];
		if ( jackpot_count < 2 )
		 sprintf (str, "%s %d", STR_JACKPOT_COUNT, 10 - bonus_count);
		else
		 sprintf (str, "%dx %s %d", jackpot_count,
						STR_JACKPOT_COUNT, 10 - bonus_count);
		message_noint (str);
	}
	else
	if ( bonus_count >= 10 )
	{
		char score[32];
		char str[32];
	
		bonus_count = 0;
		flash2(0, 255, 100, 3, 2, 2);
		
		score_sprintf(score, time_bonus*jackpot_count);
		
		if ( jackpot_count < 2 )
			sprintf (str, "((( %s: %s )))", 
					STR_TIME_BONUS,
					score);
		else
		sprintf (str, "((( %dx %s: %s )))", 
					jackpot_count, STR_TIME_BONUS,
					score);


		message_noint (str);
		value += (time_bonus * jackpot_count);
	
		jackpot_count ++;
	}
}

	game.score += value;

	/* Check for extra ball only if we're not in a jackpot sequence */
	/* They'll be won after the jackpot sequence is finished, anyway. */
	if ( (bonus_count < 4) && (jackpot_count < 2) )
		{
			score_check_extra_ball();
		}
}
/* End level bonuses */
void score_bonus_endlevel (void)
{

	long value;
	char str[32];
	char score[16];

	if ( game.nlosts == 0 )
	{
		/* Perfect bonus */
		value = PERFECT_BONUS_BASE * game.level;		
	
		message(STR_LVL_WON3);
		flash2(250, 230, 0, 300, 2, 3);
		score_sprintf(score, value);
		sprintf (str, "Bonus: %s\n", score);		
	}
	else if ( game.nlosts == 1 )
	{
		/* End bonus */
		value = BONUS_BASE * game.level;

		message(STR_LVL_WON2);
		SDL_Delay(2000);
		score_sprintf(score, value);
		sprintf (str, "Bonus: %s\n", score);		
	}else{
		/* No bonus */
		value = 0;
		strcpy(str, STR_LVL_WON1);
	}

	message(str);
	SDL_Delay(2000);
	score_raise(value);
}

void score_raise (long value)
{
	long incr;

	if ( value == 0 ) return;
	incr = value;

	/* Remind the current score */
	message_status();
	SDL_Delay(1500);

	/* Increment it */
	/* Disable jackpot */
	nojackpot = 1;
	while ( incr > 0 )
	{
		/* Include extra ball check */
		score_incr(100);
		incr	   -= 100;
		message_status();
		SDL_Delay(2);
	}
	nojackpot = 0;

	SDL_Delay(2000);
}

void score_check_extra_ball(void)
{
	static double extra_threshold = EXTRA_BALL_SCORE_BASE;

	if ( game.score >= extra_threshold )
	{
		/* Next extra ball score */
		extra_threshold *= 1.9;

		/* Flash & message */
		message (STR_EXTRA_BALL);
		flash2  (0, 255, 255, 300, 2, 3);
		flash2  (0, 255, 255, 150, 1, 3);
		flash2  (0, 255, 255, 50, 1, 3);
		flash2  (0, 255, 255, 20,  1, 10);
		game.lifes ++;
	}
}

void score_sprintf (char * str, long score)
{
	if ( score < 1E3 )
		sprintf (str, "%ld", score);
	else if ( score < 1E6 )
		sprintf (str, "%ld,%.3ld", (int)(score/1E3),
				(int)(score - 1E3*((int)(score/1E3))));
	else
		sprintf (str, "%d,%.3ld,%.3ld",
				(int)(score/1E6),
				(int)(score - 1E6*(int)(((int)(score/1E6))/1E3)),
				(int)(score/1E3),
				(int)(score - 1E3*((int)(score/1E3))));	
}

